import { notification, message } from 'antd';
import cloneDeep from 'lodash/cloneDeep';
import _map from 'lodash/map';
import _uniq from 'lodash/uniq';
import _pull from 'lodash/pull';
import { GetDateThree, FormatnumberZero, numSub } from '../../../../function';
import {
  getBonds,
  editBonds,
  removeBonds,
  addBonds,
} from '../../../../services/allposition_service';

export default {
  namespace: 'bonds',
  state: {
    bonds: { code: 0, message: '', list: [], bondTypes: [] },
  },

  effects: {
    // stock

    *GetBonds({ payload, callback }, { call, put }) {
      const response = yield call(getBonds, { bondType: payload.bondType });
      // if (payload.dataSourceType === 'tableChange') {

      // }

      if (typeof response === 'undefined') {
        return;
      }

      let todayProfit = 0;
      let totalProfit = 0;
      let todayMarketValue = 0;
      let todayPayInterest = 0;
      // if (payload.currentDataSource.length === 0) {
      //   for (let i = 0; i < response.list.data.length; i++) {
      //     todayProfit += parseFloat(response.list.data[i].todayProfit || 0);
      //     totalProfit += parseFloat(response.list.data[i].totalProfit || 0);
      //     todayMarketValue += parseFloat(response.list.data[i].todayMarketValue || 0);
      //     todayPayInterest += parseFloat(response.list.data[i].todayPayInterest || 0);
      //   }
      // } else {
      //   let bondTypeToDisplay = _pull(_uniq(_map(payload.currentDataSource, 'bondTypeToDisplay')), null, undefined);
      //   for (let i = 0; i < response.list.data.length; i++) {
      //     if (bondTypeToDisplay.indexOf(response.list.data[i].bondTypeToDisplay) !== -1) {
      //       todayProfit += parseFloat(response.list.data[i].todayProfit || 0);
      //       totalProfit += parseFloat(response.list.data[i].totalProfit || 0);
      //       todayMarketValue += parseFloat(response.list.data[i].todayMarketValue || 0);
      //       todayPayInterest += parseFloat(response.list.data[i].todayPayInterest || 0);
      //     }
      //   }
      // }
      if (payload.dataSourceType === 'tableChange') {
        for (let i = 0; i < payload.currentDataSource.length; i++) {
          if (payload.currentDataSource[i].bondTypeToDisplay === '合计') {
            continue;
          }

          todayProfit = numSub(
            todayProfit,
            -FormatnumberZero(payload.currentDataSource[i].todayProfit || 0, 2)
          );
          totalProfit = numSub(
            totalProfit,
            -FormatnumberZero(payload.currentDataSource[i].totalProfit || 0, 2)
          );
          todayMarketValue = numSub(
            todayMarketValue,
            -FormatnumberZero(payload.currentDataSource[i].todayMarketValue || 0, 2)
          );
          todayPayInterest = numSub(
            todayPayInterest,
            -FormatnumberZero(payload.currentDataSource[i].todayPayInterest || 0, 2)
          );
        }
      } else {
        for (let i = 0; i < response.list.data.length; i++) {
          todayProfit = numSub(
            todayProfit,
            -FormatnumberZero(response.list.data[i].todayProfit || 0, 2)
          );
          totalProfit = numSub(
            totalProfit,
            -FormatnumberZero(response.list.data[i].totalProfit || 0, 2)
          );
          todayMarketValue = numSub(
            todayMarketValue,
            -FormatnumberZero(response.list.data[i].todayMarketValue || 0, 2)
          );
          todayPayInterest = numSub(
            todayPayInterest,
            -FormatnumberZero(response.list.data[i].todayPayInterest || 0, 2)
          );
        }
      }
      response.list.data.unshift({
        bondTypeToDisplay: '合计',
        todayProfit,
        totalProfit,
        todayMarketValue,
        todayPayInterest,
      });
      if (typeof response !== 'undefined' && response.code === 1) {
        yield put({
          type: 'saveBonds',
          payload: {
            code: 1,
            message: response.message,
            list: response.list.data,
            bondTypes: response.list.bondTypes,
          },
        });
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *RemoveBonds({ payload, callback }, { call, put }) {
      const response = yield call(removeBonds, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (callback) callback(response);
      if (response.code == 0) {
        notification.error({
          message: response.message ? response.message : '错误!',
        });
      }
      // yield put({
      //   type: 'saveDeleteBillData',
      //   payload: response,
      // });
    },
    *EditBonds({ payload, callback }, { call, put }) {
      const response = yield call(editBonds, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (callback) callback(response);
      if (response.code == 0) {
        notification.error({
          message: response.message ? response.message : '错误!',
        });
      }
      // yield put({
      //   type: 'saveDeleteBillData',
      //   payload: response,
      // });
    },
    *AddBonds({ payload, callback }, { call, put }) {
      const response = yield call(addBonds, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (callback) callback(response);
      if (response.code == 0) {
        notification.error({
          message: response.message ? response.message : '错误!',
        });
      }
      // yield put({
      //   type: 'saveDeleteBillData',
      //   payload: response,
      // });
    },
  },

  reducers: {
    saveBonds(state, action) {
      return {
        ...state,
        bonds: action.payload,
      };
    },
  },
};
